package com.spgf.controller;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.controller.fragments.ManutencaoFragment;
import com.spgf.controller.fragments.PendenciasFragment;
import com.spgf.controller.fragments.ResumoInicialFragment;
import com.spgf.model.rn.LancamentoRN;
import com.spgf.model.utils.AndroidUtil;

public class ResumoSwipeActivity extends FragmentActivity {

	public static class SwipePadraoPagerAdapter extends FragmentStatePagerAdapter {

		public SwipePadraoPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Fragment getItem(int i) {

			switch (i) {

				case 0:
					return new ManutencaoFragment();

				case 1:
					return new ResumoInicialFragment();

				case 2:
					return new PendenciasFragment();

				default:
					return new ResumoInicialFragment();
			}
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return "";
		}
	}

	ViewPager				mViewPager;

	SwipePadraoPagerAdapter	swipePagerAdapter;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.swipe_padrao);

		swipePagerAdapter = new SwipePadraoPagerAdapter(getSupportFragmentManager());

		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(swipePagerAdapter);
		// mViewPager.setVisibility(2);
		// mViewPager.setKeepScreenOn(false);
		// mViewPager.setSystemUiVisibility(4);
		mViewPager.setCurrentItem(1);

		PagerTitleStrip lPage = (PagerTitleStrip) findViewById(R.id.pager_title_strip);
		lPage.setVisibility(8);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// Handle item selection
		switch (item.getItemId()) {
			case R.id.menu_importar_lancamento:
				try {
					new LancamentoRN(this).importarLancamento();
					Toast.makeText(this, "Despesas cadastradas com sucesso.", Toast.LENGTH_LONG).show();
				} catch (NegocioException e) {
					Toast.makeText(this, AndroidUtil.obterMensagemErro(e), Toast.LENGTH_LONG).show();
				}
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

}
